﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Text;
/// <summary>
/// CommonDAL 的摘要说明
/// </summary>
namespace DAL
{
    public class CommonDAL
    {
        static string conStr = ConfigurationManager.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationManager.AppSettings["dbPath"] + ";";
        public CommonDAL()
        {

        }
        public static PagedDataSource Pager(string tablename, string filter, int pagesize, int pageindex, ref int rowscount)
        {

            OleDbConnection objConn = new OleDbConnection(conStr);
            string sql = "select * from " + tablename + " where 1=1 " + filter +" order by id desc";
            OleDbDataAdapter objCommand = new OleDbDataAdapter(sql, objConn);
            DataSet ds = new DataSet();
            objCommand.Fill(ds);
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = ds.Tables[0].DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = pagesize; //每页文章显示的条数
            rowscount = objPds.DataSourceCount;
            objPds.CurrentPageIndex = pageindex - 1;
            return objPds;
        }
        public static PagedDataSource Pager2(string tablename, string filter, int pagesize, int pageindex, ref int rowscount)
        {
            OleDbConnection objConn = new OleDbConnection(conStr);
            string sql = "select * from " + tablename + " where 1=1 " + filter + " order by sort asc,id desc";
            OleDbDataAdapter objCommand = new OleDbDataAdapter(sql, objConn);
            DataSet ds = new DataSet();
            objCommand.Fill(ds);
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = ds.Tables[0].DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = pagesize; //每页文章显示的条数
            rowscount = objPds.DataSourceCount;
            objPds.CurrentPageIndex = pageindex - 1;
            return objPds;
        }
        public static int ExecuteNonQuery(string text)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = text;
            odc.Connection = con;
            con.Open();
            int count = odc.ExecuteNonQuery();
            con.Close();
            con.Dispose();
            return count;

        }
        public static OleDbDataReader ExecuteReader(OleDbConnection con, string text)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = text;
            odc.Connection = con;
            con.Open();
            OleDbDataReader dr = odc.ExecuteReader();
            return dr;
        }
        public static object ExecuteScalar(string text)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = text;
            odc.Connection = con;
            con.Open();
            object single = odc.ExecuteScalar();
            con.Close();
            con.Dispose();
            return single;
        }
        public static string getColumnById(int id, string columnout, string table)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "select " + columnout + " from " + table + " where id=" + id;
            odc.Connection = con;
            con.Open();
            string single = odc.ExecuteScalar() + "";
            con.Close();
            con.Dispose();
            return single;
        }
        public static int DeleteById(int id, string table)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "delete from " + table + " where id=" + id;
            odc.Connection = con;
            con.Open();
            int single = odc.ExecuteNonQuery();
            con.Close();
            con.Dispose();
            return single;
        }
        public static string CutString(string stringToSub, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    nLength += 2;
                }
                else
                {
                    nLength = nLength + 1;
                }

                if (nLength <= (length * 2))
                {
                    sb.Append(stringChar[i]);
                }
                else
                {
                    break;
                }
            }
            if (sb.ToString() != stringToSub)
            {
                sb.Append("...");
            }
            return sb.ToString();
        }
        //获得一个唯一数
        public static string getUniqueNum()
        {
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();
            string hour = DateTime.Now.Hour.ToString();
            string minute = DateTime.Now.Minute.ToString();
            string second = DateTime.Now.Second.ToString();
            string mim = DateTime.Now.Millisecond.ToString();
            return year + month + day + hour + minute + second + mim;
        }
        public static string getNum()
        {
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();
            return year + month + day;
        }
        public static string AutoNum(string type, string tablename, string fieldname)
        {
            string Num = "";
            OleDbConnection con = new OleDbConnection(conStr);
            con.Open();
            OleDbDataReader sda;
            string sql = "select Right(Max(" + fieldname + ") ,4)  from  " + tablename + "  where  " + fieldname + "  like '" + type + "%' ";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            sda = cmd.ExecuteReader();
            sda.Read();
            if (sda.GetValue(0).ToString() == "")
            {
                Num = type + "0001";
            }
            else
            {
                int num = Convert.ToInt32(sda.GetValue(0)) + 1;
                Num = type + num.ToString("0000");
            }
            sda.Dispose();
            sda.Close();
            con.Close();
            con.Dispose();
            return Num;
        }

    }

}